<?php

include FCPATH . 'application/controllers/frontend.php';

class Message extends Frontend {

    public function __construct() {
        parent::__construct();
        $this->load->model('message_model', 'model');
    }

    public function insert() {
        $data = array(
            'is_display' => 1,
	    'company' => trim($this->input->post('company')),
            'name' => trim($this->input->post('name')),
            'telephone' => trim($this->input->post('telephone')),
            'email' => trim($this->input->post('email')),
            'address' => trim($this->input->post('address')),
            'content' => trim(nl2br($this->input->post('content'))),
            'ip' => $this->input->ip_address(),
            'created_at' => date('Y-m-d H:i:s', time())
        );
        if (empty($data['name'])) {
            $this->json_back('XT002', '请填写您的称呼');
        }
        if (empty($data['telephone'])) {
            $this->json_back('XT002', '请填写您的电话');
        }
        if (empty($data['email'])) {
            $this->json_back('XT002', '请填写您的邮箱');
        }
        if (empty($data['content'])) {

            $this->json_back('XT002', '请填写您的留言内容');
        }
        if (($id = $this->model->insert($data)) > 0) {
            $this->json_back('XT001', '新增留言成功', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

}